import Vue from 'vue'
import Router from 'vue-router'
const Login = resolve => require(['@/components/AG_Login'], resolve);
const Index = resolve => require(['@/components'], resolve);

const Integration = resolve => require(['@/components/AG_Integration'], resolve);
const Advert = resolve => require(['@/components/AG_Advert'], resolve);

const Reward = resolve => require(['@/components/AG_Reward'], resolve);

const Apply = resolve => require(['@/components/AG_Apply'], resolve);


const Lives = resolve => require(['@/components/AG_Lives'], resolve);

const PearlExchange = resolve => require(['@/components/AG_Pearl'], resolve);

const Pushed = resolve => require(['@/components/AG_Pushed'], resolve);

const Shopping = resolve => require(['@/components/AG_Shopping'], resolve);
const ShoppingEditPage = resolve => require(['@/components/AG_Shopping/shoppingEditPage.vue'], resolve);
const ShoppingView = resolve => require(['@/components/AG_Shopping/shoppingView.vue'], resolve);

const Home = resolve => require(['@/components/AG_Home'], resolve);
const EditHome = resolve => require(['@/components/AG_Home/editHome.vue'], resolve);

const ErrorHtml = resolve => require(['@/components/AG_Error'], resolve);


Vue.use(Router)

const router = new Router({
    mode: 'history',
    routes: [{
            path: '/',
            name: 'login',
            component: Login
        },
        {
            path: '/main',
            name: 'main',
            component: Index,
            redirect:{
              name: 'home',
            },
            children: [{
                    path: 'home',
                    name: 'home',

                    component: Home,
                    props: { text:'首页' }
                },
                {
                  path: 'editHome',
                  name: 'editHome',
                  component: EditHome,
                },
                {
                    path: 'pushed',
                    name: 'pushed',
                    component: Pushed,
                },
                {
                    path: 'shopping',
                    name: 'shopping',
                    component: Shopping,
                },
                {
                    path: 'shoppingEditPage',
                    name: 'shoppingEditPage',
                    component: ShoppingEditPage,
                },
                {
                  path: 'shoppingView',
                  name: 'shoppingView',
                  component: ShoppingView,
                },
                {
                    path: 'reward',
                    name: 'reward',
                    component: Reward,
                },
                {
                  path: 'apply',
                  name: 'apply',
                  component: Apply,
                },
                {
                    path: 'advert',
                    name: 'advert',
                    component: Advert,
                },
                {
                    path: 'lives',
                    name: 'lives',
                    component: Lives,
                },
                {
                  path: 'pearlExchange',
                  name: 'pearlExchange',
                  component: PearlExchange
                },
                {
                  path: 'errorHtml',
                  name: 'errorHtml',
                  component: ErrorHtml
                }
            ]
        }
    ]
})
// 路由跳转前验证
router.beforeEach((to, from, next) => {
  // console.log(to.matched, from.name)
  if (to.matched.length === 0) { //如果未匹配到路由
    next({ name: 'errorHtml' }) ; //如果上级也未匹配到路由则跳转登录页面，如果上级能匹配到则转上级路由
  } else {
    next();
  }
});
export default router;
